package com.ruoyi.project.cms.taskdelayask.service;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.task.domain.Assignment;
import com.ruoyi.project.cms.task.mapper.AssignmentMapper;
import com.ruoyi.project.cms.taskdelayask.domain.Delayapply;
import com.ruoyi.project.cms.taskdelayask.mapper.DelayapplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DelayapplyServiceImpl implements DelayapplyService {

    @Autowired
    private DelayapplyMapper delayapplyMapper;

    @Autowired
    private AssignmentMapper assignmentMapper;

    @Override
    public List<Delayapply> selectAll(Delayapply delayapply , String roleName) {

        return delayapplyMapper.selectAll(delayapply,roleName);
    }

    @Override
    public int addSava(Delayapply delayapply) {

        String title = delayapply.getTitle();
        Date date = new Date();
        delayapply.setCdate(date);
        Assignment select = assignmentMapper.select(title);
        if (select.getId()!=0){
            delayapply.setId(select.getId());
            int insert = delayapplyMapper.insert(delayapply);
            String aname = delayapply.getAname();
            int aid = delayapplyMapper.selectOne(aname);
            delayapply.setAid(aid);
            int add = delayapplyMapper.add(delayapply);
            return insert*add;
        }
        return 0;
    }

    @Override
    public int  delect(int ids) {

        int cid = delayapplyMapper.find(ids);
        int delect = delayapplyMapper.delect(ids);
        int i = delayapplyMapper.delectCh(cid);
        return delect*i;
    }

    @Override
    public Delayapply selectOne(int id) {

        return delayapplyMapper.select(id);
    }

    @Override
    public int update(Delayapply delayapply) {
        String title = delayapply.getTitle();
        Assignment select = assignmentMapper.select(title);
        if (delayapply.getId()==select.getId()){
            int update = update(delayapply);
            return update;
        }else{
            delayapply.setId(select.getId());
            System.out.println(select.toString()+"========");
            int i = assignmentMapper.updateAssignment(select);
            int update = delayapplyMapper.update(delayapply);
            return i*update;
        }
    }

    @Override
    public Assignment findDate(int id) {
        Delayapply select = delayapplyMapper.select(id);
        int id1 = select.getId();
        Assignment assignment = assignmentMapper.selectOne(id1);
        return assignment;
    }

    @Override
    public int updateDelay(Delayapply delayapply) {

        Assignment select = assignmentMapper.select(delayapply.getTitle());
        int id = select.getId();
        delayapply.setId(id);
        System.out.println(delayapply.toString()+"===========");
        if (delayapply.getAstatus()==1){
            //不同意 审批失败（修改批复表中的状态和批复时间和审批意见)
            int cid = delayapplyMapper.find(delayapply.getAid());

            int i = delayapplyMapper.updateDelay(delayapply, cid);
            return i;
        }else if(delayapply.getAstatus()==0){
            //同意 （修改批复表中的状态和批复时间和审批意见和审核中的endDate 还有任务表中的endDate）
            int cid = delayapplyMapper.find(delayapply.getAid());

            int i = delayapplyMapper.updateDelay(delayapply, cid);

            int update = delayapplyMapper.update(delayapply);

            Date endDate = delayapply.getEndDate();
            int update1 = assignmentMapper.update(endDate);
            return i*update*update1;
        }else{
            return 0;
        }

    }
}
